package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "菜品相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result AddSort(@RequestBody CategoryDTO categoryDTO){
        log.info("新增菜品：{}",categoryDTO.toString());
        categoryService.addSort(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分类分页查询")
    public Result<PageResult> select(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询信息：{}",categoryPageQueryDTO.toString());
        PageResult pageResult = categoryService.select(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation(value = "根据id删除分类")
    public Result deleteById(Integer id){
        log.info("要删除的菜品Id是：{}",id);
        categoryService.deteleById(id);
        return Result.success();
    }

    /**
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("要修改的分类：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用禁用分类")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("状态改为：{}",status);
        categoryService.startOrStop(id,status);
        return Result.success();
    }

    /**
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据类型查询分类")
    public Result listByType(Integer type){
        log.info("根据类型查询分类，类型为：{}",type);
        List<Category> list = categoryService.listByType(type);
        return Result.success(list);
    }
}
